<?php

declare(strict_types=1);

namespace App\Admin\Controller\Setting;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Request\Tool\GenerateUpdateRequest;
use App\Admin\Request\Tool\LoadTableRequest;
use App\Admin\Service\Setting\SettingGenerateColumnsService;
use App\Admin\Service\Setting\SettingGenerateTablesService;
use DI\Attribute\Inject;
use DI\DependencyException;
use DI\NotFoundException;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\DeleteMapping;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PostMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use support\Request;
use support\Response;

/**
 * 代码生成器控制器
 * Class CodeController
 * @package App\Setting\Controller\Tools
 */
#[Controller(prefix: "setting/code"), Auth]
class GenerateCodeController extends NewbieController {
    /**
     * 信息表服务
     */
    #[Inject]
    protected SettingGenerateTablesService $tableService;

    /**
     * 信息字段表服务
     */
    #[Inject]
    protected SettingGenerateColumnsService $columnService;

    /**
     * 代码生成列表分页
     * @param Request $request
     * @return Response
     */
    #[GetMapping("index"), Permission("setting:code")]
    public function index (Request $request): Response {
        return $this->success($this->tableService->getPageList($request->All()));
    }

    /**
     * 获取业务表字段信息
     * @param Request $request
     * @return Response
     */
    #[GetMapping("getTableColumns")]
    public function getTableColumns (Request $request): Response {
        return $this->success($this->columnService->getList($request->all()));
    }

    /**
     * 预览代码
     * @param Request $request
     * @return Response
     * @throws DependencyException
     * @throws NotFoundException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping("preview"), Permission("setting:code:preview")]
    public function preview (Request $request): Response {
        return $this->success($this->tableService->preview((int)$request->input('id', 0)));
    }

    /**
     * 读取表数据
     * @param Request $request
     * @return Response
     */
    #[GetMapping("readTable")]
    public function readTable (Request $request): Response {
        return $this->success($this->tableService->read((int)$request->input('id')));
    }

    /**
     * 更新业务表信息
     * @param GenerateUpdateRequest $request
     * @return Response
     */
    #[PostMapping("update"), Permission("setting:code:update")]
    public function update (GenerateUpdateRequest $request): Response {
        return $this->tableService->updateTableAndColumns($request->validated()) ? $this->success() : $this->error();
    }

    /**
     * 生成代码
     * @param String $ids
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("generate/{ids}"), Permission("setting:code:generate")]
    public function generate (string $ids): Response {
        return $this->_download($this->tableService->generate($ids), 'newbieadmin.zip');
    }

    /**
     * 加载数据表
     * @param LoadTableRequest $request
     * @return Response
     */
    #[PostMapping("loadTable"), Permission("setting:code:loadTable")]
    public function loadTable (LoadTableRequest $request): Response {
        return $this->tableService->loadTable($request->validated()['names']) ? $this->success() : $this->error();
    }

    /**
     * 删除代码生成表
     * @param string $ids
     * @return Response
     */
    #[DeleteMapping("delete/{ids}"), Permission("setting:code:delete")]
    public function delete (string $ids): Response {
        return $this->tableService->delete($ids) ? $this->success() : $this->error();
    }

    /**
     * 同步数据库中的表信息跟字段
     * @param int $id
     * @return Response
     */
    #[PutMapping("sync/{id}"), Permission("setting:code:sync")]
    public function sync (int $id): Response {
        return $this->tableService->sync($id) ? $this->success() : $this->error();
    }

    /**
     * 获取所有启用状态模块下的所有模型
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping("getModels")]
    public function getModels (): Response {
        return $this->success($this->tableService->getModels());
    }
}